class Libadwaita < Formula
  desc "Building blocks for modern adaptive GNOME applications"
  homepage "https://gnome.pages.gitlab.gnome.org/libadwaita/"
  url "https://download.gnome.org/sources/libadwaita/1.8/libadwaita-1.8.1.tar.xz"
  sha256 "8b1d4d5f89373a5b6eea8d93ecdaee34cd26ad27a4dd396c06f5d9929fb313bc"
  license "LGPL-2.1-or-later"
  head "https://gitlab.gnome.org/GNOME/libadwaita.git", branch: "main"

  # libadwaita doesn't use GNOME's "even-numbered minor is stable" version
  # scheme. This regex is the same as the one generated by the `Gnome` strategy
  # but it's necessary to avoid the related version scheme logic.
  livecheck do
    url :stable
    regex(/libadwaita-(\d+(?:\.\d+)+)\.t/i)
  end

  bottle do
    sha256 arm64_tahoe:   "53e5079274d61d7134bb01c5fabeea9171d87bb8d57106850a70d52e2947ebc2"
    sha256 arm64_sequoia: "ac5347de5fc072b088dc7507b6e15fb0f8ef3ea25a4a711b52c95da4d070b77c"
    sha256 arm64_sonoma:  "4ac1a7cf13f46c304fbb18be8f2ada02c96a3cee1eab3315bea803a7581836c0"
    sha256 sonoma:        "543de40823426f9f0f8655e546ddefb3f15ee79533de346ecf2b415d9b3a92c2"
    sha256 arm64_linux:   "bc06cab285fab6ded585a35f614537ce8189b81b055d3a0cd42c6f20495487c0"
    sha256 x86_64_linux:  "6f565b49ddd5131bbf50575ddda6fe8a3a3efe3f653f6baf921f3959e932ec89"
  end

  depends_on "gettext" => :build
  depends_on "gobject-introspection" => :build
  depends_on "meson" => :build
  depends_on "ninja" => :build
  depends_on "pkgconf" => [:build, :test]
  depends_on "sassc" => :build
  depends_on "vala" => :build

  depends_on "appstream"
  depends_on "fribidi"
  depends_on "glib"
  depends_on "graphene"
  depends_on "gtk4"
  depends_on "libsass"
  depends_on "pango"

  uses_from_macos "python" => :build

  on_macos do
    depends_on "gettext"
  end

  def install
    system "meson", "setup", "build", "-Dtests=false", *std_meson_args
    system "meson", "compile", "-C", "build", "--verbose"
    system "meson", "install", "-C", "build"
  end

  test do
    (testpath/"test.c").write <<~C
      #include <adwaita.h>

      int main(int argc, char *argv[]) {
        g_autoptr (AdwApplication) app = NULL;
        app = adw_application_new ("org.example.Hello", G_APPLICATION_DEFAULT_FLAGS);
        return g_application_run (G_APPLICATION (app), argc, argv);
      }
    C
    flags = shell_output("#{Formula["pkgconf"].opt_bin}/pkgconf --cflags --libs libadwaita-1").strip.split
    system ENV.cc, "test.c", "-o", "test", *flags
    system "./test", "--help"

    # include a version check for the pkg-config files
    assert_match version.to_s, (lib/"pkgconfig/libadwaita-1.pc").read
  end
end
